﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;


namespace SysObj
{
    public class Kho_PhieuXuat : Lib.Bases.BaseClass
    {
        Kho_PhieuXuat_HangHoa px_hh = new Kho_PhieuXuat_HangHoa();

        #region Prop
        public System.Int32 IdPhieuXuat
        {
            get;
            set;
        }

        public System.DateTime Ngay
        {
            get;
            set;
        }

        public System.String So
        {
            get;
            set;
        }

        public System.Int32 IdKhachHang
        {
            get;
            set;
        }

        public System.String NguoiXuat
        {
            get;
            set;
        }

        public System.String DienGiai
        {
            get;
            set;
        }

        #endregion

        public DateTime F_FromDate { get; set; }
        public DateTime F_ToDate { get; set; }
        public bool F_Detail { get; set; }

        public DataTable D_TX { get; set; }

        #region  Constructors
        public Kho_PhieuXuat()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }

        #endregion

        #region Methods
        public override void ResetData()
        {
            IdPhieuXuat = 0;
            Ngay = DateTime.Now;
            So = string.Empty;
            IdKhachHang = 0;
            NguoiXuat = string.Empty;
            DienGiai = string.Empty;
        }

        public override DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_PhieuXuat", "Command,TuNgay,DenNgay,F_Detail", "LoadData", Cvert.ToShortDate(F_FromDate), Cvert.ToShortDate(F_ToDate), F_Detail);
        }

        public DataTable FListByCustomerID()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_PhieuXuat", "Command,IdKhachHang,TuNgay,DenNgay", "FListByCustomerID", IdKhachHang, Cvert.ToShortDate(F_FromDate), Cvert.ToShortDate(F_ToDate));
        }

        public DataTable GetDataPrint()
        {
            return m_DataLayer.ExecDataTable("SP_Kho_PhieuXuat", "Command,IdPhieuXuat", "GetDataPrint", IdPhieuXuat);
        }

        public void ReadData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = m_DataLayer.ExecDataTable("SP_Kho_PhieuXuat", "Command,IdPhieuXuat", "ReadData", IdPhieuXuat);
                this.ResetData();
                if (dt == null || dt.Rows.Count <= 0)
                    return;
                this.FillData(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public override void FillData(DataRow row)
        {
            ResetData();
            if (row.Table.Columns.Contains("IdPhieuXuat"))
                IdPhieuXuat = Lib.Data.ValueType.GetValue(row["IdPhieuXuat"], IdPhieuXuat);
            if (row.Table.Columns.Contains("Ngay"))
                Ngay = Lib.Data.ValueType.GetValue(row["Ngay"], Ngay);
            if (row.Table.Columns.Contains("So"))
                So = Lib.Data.ValueType.GetValue(row["So"], So);
            if (row.Table.Columns.Contains("IdKhachHang"))
                IdKhachHang = Lib.Data.ValueType.GetValue(row["IdKhachHang"], IdKhachHang);
            if (row.Table.Columns.Contains("NguoiXuat"))
                NguoiXuat = Lib.Data.ValueType.GetValue(row["NguoiXuat"], NguoiXuat);
            if (row.Table.Columns.Contains("DienGiai"))
                DienGiai = Lib.Data.ValueType.GetValue(row["DienGiai"], DienGiai);
            if (row.Table.Columns.Contains("AddedBy"))
                AddedBy = Lib.Data.ValueType.GetValue(row["AddedBy"], AddedBy);
        }

        public string GetNo(DateTime Date)
        {
            return Cvert.ToString(m_DataLayer.ExecScalar("SP_KHO_PHIEUXUAT", "Command,Ngay", "GetNo", Cvert.ToShortDate(Date)), "");
        }

        public DataTable Get_ThucXuat()
        {
            return px_hh.GetDetail(m_DataLayer, IdPhieuXuat);
        }

        public override void OnUpdate()
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Update");
                Lib.Data.ParamHolder.AddParam("IdPhieuXuat", IdPhieuXuat);
                Lib.Data.ParamHolder.AddParam("Ngay", Cvert.ToShortDate(Ngay));
                Lib.Data.ParamHolder.AddParam("So", So);
                Lib.Data.ParamHolder.AddParam("IdKhachHang", IdKhachHang);
                Lib.Data.ParamHolder.AddParam("NguoiXuat", NguoiXuat);
                Lib.Data.ParamHolder.AddParam("DienGiai", DienGiai);
                Lib.Data.ParamHolder.AddParam("UserID", ConnInfo.UserLoginID);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_KHO_PHIEUXUAT", s_ParamName, ref output, o_ParamValue);

                if (!object.Equals(null, output))
                    IdPhieuXuat = Cvert.ToInt32(output["IdPhieuXuat"], 0);

                px_hh.OnDelete(m_DataLayer, IdPhieuXuat);

                px_hh.Insert(m_DataLayer, D_TX, IdPhieuXuat);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete()
        {
            string s_ParamName = "";
            object[] o_ParamValue = null;
            try
            {
                m_DataLayer.BeginTransaction();

                px_hh.OnDelete(m_DataLayer, IdPhieuXuat);

                Lib.Data.ParamHolder.ClearAll();
                Lib.Data.ParamHolder.AddParam("Command", "Delete");
                Lib.Data.ParamHolder.AddParam("IdPhieuXuat", IdPhieuXuat);
                Lib.Data.ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                m_DataLayer.ExecNonQuery("SP_KHO_PHIEUXUAT", s_ParamName, o_ParamValue);

                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }


        #endregion
    }
}
